//
//  StarCalculation.m
//  MyPopStar
//
//  Created by tarena on 16/1/6.
//  Copyright © 2016年 ssnb. All rights reserved.
//

#import "StarCalculation.h"

@implementation StarCalculation
mdealloc

//新游戏生成随机数组
+(allStarArray)getInitGameArray{
//    NSLog(@"log");
    allStarArray gameArray = [NSMutableArray arrayWithCapacity:10];
    for (int i =0; i<10; i++) {
        columnArray column =[NSMutableArray arrayWithCapacity:10];
        for (int j = 0; j<10; j++) {
            StarModel *row = [[StarModel alloc]init];
            row.starModelType = arc4random()%5;
            row.starPoint = StarPointMake(i,j);
            row.choose = NO;
            [column addObject:row];
        }
        [gameArray addObject:column];
    }
    return  gameArray;
}

+(allStarArray)getResumGameArray{
    NSString *filePath = [DocumentPath stringByAppendingPathComponent:@"resume"];
    
    //将自定义的TRPerson对象进行解档(读)
    //1.读取数据
    NSData *readingData = [NSData dataWithContentsOfFile:filePath];
    //2.解档对象
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:readingData];
    //3.解码
    allStarArray arr = [unarchiver decodeObjectForKey:@"da"];
    //4.完成解码
    [unarchiver finishDecoding];
    
    return arr;
}
+(void)saveResumGameArray:(allStarArray)arr{
    
    NSString *filePath = [DocumentPath stringByAppendingPathComponent:@"resume"];
    
    //1.可变数据
    NSMutableData *data = [NSMutableData data];
    //2.归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3.编码
    [archiver encodeObject:arr forKey:@"da"];
    //4.完成编码
    [archiver finishEncoding];
    //5.写入文件
    [data writeToFile:filePath atomically:YES];
    
    
//    [dic writeToFile:filePath atomically:YES];
    
}
#pragma 判断选中点周围是否有相连的
+(BOOL)canChoose:(allStarArray) gameArray atPoint:(StarPoint)point{
    if ([StarCalculation isTopOK:gameArray atPoint:point]||[StarCalculation isLeftOK:gameArray atPoint:point]||[StarCalculation isRightOK:gameArray atPoint:point]||[StarCalculation isBottomOK:gameArray atPoint:point]) {
        return  YES;
    }
    return NO;
}
/**
 *  @author ssnb, 16-01-08 12:01:35
 *
 * //如果当前位置不是在最上方那么上方就由对象
 *
 *  @param gameArray <#gameArray description#>
 *  @param point     <#point description#>
 *
 *  @return <#return value description#>
 */
+(BOOL)isTopOK:(allStarArray) gameArray atPoint:(StarPoint)point{
    StarModel *clickStar =[[gameArray objectAtIndex:(point.column)]objectAtIndex:(point.row)];
    
    if (point.row<([gameArray objectAtIndex:point.column].count-1)) {
        StarModel *topStar = [[gameArray objectAtIndex:point.column] objectAtIndex:(point.row+1)];
        if (clickStar.starModelType == topStar.starModelType) {
            if (topStar.choose) {
                return NO;
            }
            return  YES;
        }
    }
    return  NO;
}
/**
 *  @author ssnb, 16-01-08 12:01:11
 *
 *
//如果 column -1>=0就是说明左边有数组
//如果左边数组的高度>当前对象的高度,说明左边有值
 *
 *  @param gameArray <#gameArray description#>
 *  @param point     <#point description#>
 *
 *  @return <#return value description#>
 */
+(BOOL)isLeftOK:(allStarArray) gameArray atPoint:(StarPoint)point{
    StarModel *clickStar =[[gameArray objectAtIndex:(point.column)]objectAtIndex:(point.row)];
    if(point.column-1>=0){
        if ([gameArray objectAtIndex:(point.column-1)].count>point.row) {
            StarModel *leftStar = [[gameArray objectAtIndex:(point.column-1)]objectAtIndex:(point.row)];
            
            if (clickStar.starModelType == leftStar.starModelType) {
                if (leftStar.choose) {
                    return NO;
                }
                return  YES;
            }
        }
    }
    return  NO;
}
/**
 *  @author ssnb, 16-01-08 12:01:23
 *
 *      //如果column+1<所有竖排数,说明右边还有数组
 //右边值判断同左边值判断
 *
 *  @param gameArray <#gameArray description#>
 *  @param point     <#point description#>
 *
 *  @return <#return value description#>
 */
+(BOOL)isRightOK:(allStarArray) gameArray atPoint:(StarPoint)point{
    StarModel *clickStar =[[gameArray objectAtIndex:(point.column)]objectAtIndex:(point.row)];

    if ((point.column+1)<gameArray.count) {
        if([gameArray objectAtIndex:(point.column+1)].count>point.row){
            StarModel *rightStar = [[gameArray objectAtIndex:(point.column+1)]objectAtIndex:(point.row)];
            if (clickStar.starModelType == rightStar.starModelType) {
                if (rightStar.choose) {
                    return NO;
                }
                return  YES;
            }
        }
    }
    return  NO;
}
/**
 *  @author ssnb, 16-01-08 12:01:32
 *
 *
 //如果当前下标>0说明下面有值
 *
 *  @param gameArray <#gameArray description#>
 *  @param point     <#point description#>
 *
 *  @return <#return value description#>
 */
+(BOOL)isBottomOK:(allStarArray) gameArray atPoint:(StarPoint)point{
    StarModel *clickStar =[[gameArray objectAtIndex:(point.column)]objectAtIndex:(point.row)];

    if(point.row>0){
        StarModel *bottomStar = [[gameArray objectAtIndex:point.column] objectAtIndex:(point.row-1)];
        if (clickStar.starModelType == bottomStar.starModelType) {
            if (bottomStar.choose) {
                return NO;
            }
            return  YES;
        }
    }
    return  NO;
}

#pragma 返回周围相邻的点数组
+(chooseArray)connectedStarModel:( allStarArray)gameArray atPoint:(StarPoint)point{
    chooseArray arr= [[NSMutableArray alloc]init];
    allStarArray game = gameArray;
    game = [StarCalculation choosePoint:game withPoint:point];
    for (columnArray colum in game) {
        for (StarModel *row in colum) {
            if (row.isChoose) {
                [arr addObject:row];
            }
        }
    }
    game =nil;
    return arr;
}

+(allStarArray)choosePoint:(allStarArray)gameArr withPoint:(StarPoint)point{
    allStarArray arr=gameArr;
    [[arr objectAtIndex:point.column]objectAtIndex:point.row].choose=YES;
    if ([StarCalculation isTopOK:arr atPoint:point]) {
        arr= [StarCalculation choosePoint:arr withPoint:StarPointMake(point.column, point.row+1)];
    }
    
    if ([StarCalculation isLeftOK:arr atPoint:point]) {
        arr= [StarCalculation choosePoint:arr withPoint:StarPointMake(point.column-1, point.row)];
    }
    
    if ([StarCalculation isRightOK:arr atPoint:point]) {
        arr= [StarCalculation choosePoint:arr withPoint:StarPointMake(point.column+1, point.row)];
    }
    
    if ([StarCalculation isBottomOK:arr atPoint:point]) {
        arr= [StarCalculation choosePoint:arr withPoint:StarPointMake(point.column, point.row-1)];
    }
    return arr;
}

#pragma  mark -- 返回消除后的位置
+(allStarArray)clean:(allStarArray)gameArray and:(chooseArray)chooseArr{
    for (StarModel *mo in chooseArr) {
        [[gameArray objectAtIndex:mo.starPoint.column] removeObject:mo];
    }
    gameArray =[StarCalculation updatAdress:gameArray];
    return gameArray;

}

+(allStarArray)updatAdress:(allStarArray)gameArray{
    
    for (int i = 0; i<gameArray.count; i++) {//进行更新位置
        columnArray colum = [gameArray objectAtIndex:i];
        for (int j = 0 ; j<colum.count;j++ ) {
            [colum objectAtIndex:j].starPoint=StarPointMake(i, j);
        }
    }
    return gameArray;
}
+(BOOL)canMoveLeft:(allStarArray)gameArray{
    for (NSInteger i = gameArray.count-1 ; i>=0;i--) {
        if ([gameArray objectAtIndex:i].count==0) {
            return YES;
        }
    }
    return NO;
}

+(allStarArray)moveLeft:(allStarArray)gameArray{
    for (NSInteger i = gameArray.count-1 ; i>=0;i--) {
        if ([gameArray objectAtIndex:i].count==0) {
            [gameArray removeObjectAtIndex:i];
        }
    }
    gameArray=[StarCalculation updatAdress:gameArray];
    return  gameArray;
}

+(NSInteger)scoreWithNumber:(NSInteger)count{
    return 5*count*count;
}

+(NSInteger)gameOverScore:(NSInteger)count{
    if (count>10) {
        return 0;
    }
    NSInteger number=[self count:count];
    return 20*(100-number);
}
+(NSInteger)count:(NSInteger)count{
    if (count==0) {
        return 0;
    }
    return 2*count-1+[self count:count-1];
}
@end
